/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Xiaobao Zeng<zengxiaobao@skyworth.com>
 * Created Time: 2016-09-23 16:24:25
 * Last Modified: 2017-04-20 11:57:58
 * File Name: advertoper_admin_saverecord.go
 * Description:
 */
package adoper

import (
	//	"encoding/json"
	//	"strconv"
	"strings"
	"time"

	"advertoper/db"
	"advertoper/errorcode"

	"doubimeizhi.com/datatable"
	"doubimeizhi.com/utility"

	log "github.com/cihub/seelog"
)

func (p *AdvertoperAdminService) SaveItemRecord(req *PositionDataInfo) uint {
	tm := time.Now().Unix()
	dti := datatable.NewDataTableInsert()

	dti.SetTableName("advert_oper_redesign")
	dti.SetInsertValue("type", req.Type)
	dti.SetInsertValue("start_tick", req.StartTick)
	dti.SetInsertValue("end_tick", req.EndTick)
	dti.SetInsertValue("clickable", req.Clickable)
	dti.SetInsertValue("router", req.Router)
	dti.SetInsertValue("insert_tick", tm)
	dti.SetInsertValue("update_tick", tm)

	switch req.Type {
	case 1, 4:
		{
			dti.SetInsertValue("content", req.Content)
		}
	case 2:
		{
			if strings.Contains(req.Content, "}{") {
				req.Content = strings.Replace(req.Content, "}{", "},{", -1)
			}

			dti.SetInsertValue("pic", req.Content)
		}
	case 3:
		{
			dti.SetInsertValue("web_url", req.Content)

		}
	}
	sql, values := dti.GenerateSQL()
	mysqlHelper := db.GetMysqlHelper()
	if _, err := mysqlHelper.Exec(sql, values...); err != nil {
		log.Warnf("SaveItemRecord mysql err, :%s", err.Error())
		return errorcode.EC_GL_INTERNAL_ERROR
	}
	return errorcode.EC_GL_SUCCESS
}

func (p *AdvertoperAdminService) SaveLayoutRecord(req *LayoutInfo) uint {
	tm := time.Now().Unix()
	data := make(map[string]*utility.MySQLFieldValue)
	data["appkey"] = utility.NewMySQLFieldValue(req.Appkey, false)
	data["plat"] = utility.NewMySQLFieldValue(req.Plat, false)
	data["position"] = utility.NewMySQLFieldValue(req.Position, false)
	data["position_desc"] = utility.NewMySQLFieldValue(req.PositionDesc, false)
	data["layout"] = utility.NewMySQLFieldValue(req.Layout, false)
	data["update_tick"] = utility.NewMySQLFieldValue(tm, false)
	data["insert_tick"] = utility.NewMySQLFieldValue(tm, false)
	mysqlHelper := db.GetMysqlHelper()
	if _, err := mysqlHelper.SaveRecord(data, "advert_layout"); err != nil {
		log.Errorf("SaveLayoutRecord, error: %s, appkey: %s, pos:%d", err.Error(), req.Appkey, req.Position)
		return errorcode.EC_GL_INTERNAL_ERROR
	}
	return errorcode.EC_GL_SUCCESS
}

//修改广告位展示位置信息
func (p *AdvertoperAdminService) ModifyLayout(req *LayoutInfo) uint {
	data := make(map[string]*utility.MySQLFieldValue)
	data["appkey"] = utility.NewMySQLFieldValue(req.Appkey, false)
	data["position"] = utility.NewMySQLFieldValue(req.Position, false)
	data["plat"] = utility.NewMySQLFieldValue(req.Plat, true)
	data["position_desc"] = utility.NewMySQLFieldValue(req.PositionDesc, true)
	data["layout"] = utility.NewMySQLFieldValue(req.Layout, true)
	data["update_tick"] = utility.NewMySQLFieldValue(time.Now().Unix(), true)
	mysqlHelper := db.GetMysqlHelper()
	if _, err := mysqlHelper.SaveRecord(data, "advert_layout"); err != nil {
		log.Errorf("SaveLayoutRecord, error: %s, appkey: %s, pos:%d", err.Error(), req.Appkey, req.Position)
		return errorcode.EC_GL_INTERNAL_ERROR
	}
	return errorcode.EC_GL_SUCCESS
}
